package org.dromara.web.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.system.domain.vo.TrtcPinStatusVo;
import org.dromara.system.service.ITrtcPinStatusService;
import org.dromara.web.properties.TrtcProperties;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * TRTC 置顶控制器
 *
 * @author aaron
 */
@Slf4j
@SaIgnore
@RestController
@RequiredArgsConstructor
@RequestMapping("/trtc/pin")
public class TrtcPinController {

    private final TrtcProperties trtcProperties;
    private final ITrtcPinStatusService trtcPinStatusService;

    /**
     * 设置用户置顶状态
     *
     * @param roomId 房间ID
     * @param userId 用户ID
     * @param isPinned 是否置顶
     * @return 处理结果
     */
    @PostMapping("/set")
    public R<Void> setPinStatus(
        @RequestParam String roomId,
        @RequestParam String userId,
        @RequestParam boolean isPinned
    ) {
        log.info("设置用户置顶状态: roomId={}, userId={}, isPinned={}", roomId, userId, isPinned);
        try {
            if (StringUtils.isAnyBlank(roomId, userId)) {
                return R.fail("房间ID和用户ID不能为空");
            }

            boolean success = trtcPinStatusService.setPinStatus(roomId, userId, isPinned);
            return success ? R.ok() : R.fail("设置置顶状态失败");
        } catch (IllegalArgumentException e) {
            log.warn("设置用户置顶状态参数错误: {}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("设置用户置顶状态失败", e);
            return R.fail("设置置顶状态失败");
        }
    }

    /**
     * 获取房间内所有用户的置顶状态
     *
     * @param roomId 房间ID
     * @return 用户置顶状态列表，包含置顶状态和时间信息
     */
    @GetMapping("/list")
    public R<List<TrtcPinStatusVo>> getPinStatusList(@RequestParam String roomId) {
        log.info("获取房间置顶状态列表: roomId={}", roomId);
        try {
            if (StringUtils.isBlank(roomId)) {
                return R.fail("房间ID不能为空");
            }

            List<TrtcPinStatusVo> pinStatusList = trtcPinStatusService.getPinStatusList(roomId);
            return R.ok(pinStatusList);
        } catch (IllegalArgumentException e) {
            log.warn("获取房间置顶状态列表参数错误: {}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("获取房间置顶状态列表失败", e);
            return R.fail("获取置顶状态列表失败");
        }
    }

    /**
     * 取消用户置顶状态
     *
     * @param roomId 房间ID
     * @param userId 用户ID
     * @return 处理结果
     */
    @PostMapping("/cancel")
    public R<Void> cancelPinStatus(
        @RequestParam String roomId,
        @RequestParam String userId
    ) {
        log.info("取消用户置顶状态: roomId={}, userId={}", roomId, userId);
        try {
            if (StringUtils.isAnyBlank(roomId, userId)) {
                return R.fail("房间ID和用户ID不能为空");
            }

            boolean success = trtcPinStatusService.cancelPinStatus(roomId, userId);
            return success ? R.ok() : R.fail("取消置顶状态失败");
        } catch (IllegalArgumentException e) {
            log.warn("取消用户置顶状态参数错误: {}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("取消用户置顶状态失败", e);
            return R.fail("取消置顶状态失败");
        }
    }
}
